const app = require('j7/app');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const log = require('j7/log');
const BaseTask = require('./basetask');
const factory = require('./factory');
const serviceFactory = require('../services/factory');
const metaFactory = require('../metadata/factory');

class BcRefresh extends BaseTask {

  async init() {
    const netList = metaFactory.getNetList();
    netList.forEach
    (
      (net) => {
       this.initNet(net);
     });
  }

  async initNet(net) {
    {
      const refreshers = [];
      net['refresh_erc721'].forEach
      (
        (conf) => {
          const refresher = {
            'conf': conf,
            'progressInfo': {
              'refreshedCount': 0,
              'pendingCount': 0,
              'skipCount': 0,
            }
          };
          refreshers.push(refresher);
          this.createErc721Refresher(net, refresher);
        });
      this.outputProgressInfo(net, refreshers);
    }
    {
      const refreshers = [];
      net['refresh_erc1155'].forEach
      (
        (conf) => {
        });
    }
    {
      const refreshers = [];
      net['refresh_erc20'].forEach
      (
        (conf) => {
        });
    }
  }

  createErc721Refresher(net, refresher) {
    const bc = serviceFactory.create('BlockChain');
    bc.init(net['net_id']);
    const refresherService = serviceFactory.create('Erc721Refresher');
    refresher['refresherService'] = refresherService;
    refresherService.init(bc, net, refresher);
    return refresherService;
  }

  async outputProgressInfo(net, refreshers) {
    let count = 0;
    while (true) {
      log.info('----------------------------------------------------------');
      refreshers.forEach
      (
        (refresher) => {
          const conf = refresher['conf'];
          const progInfo = refresher['progressInfo'];
          const logObj = 'net_id: ' + net['net_id'] + ' ' +
                conf['contract_name'] + '' +
                ' refreshedCount:' + progInfo['refreshedCount'] +
                ' pendingCount:' + progInfo['pendingCount'] +
                ' skipCount:' + progInfo['skipCount']
                ;
          log.info(logObj);
        }
      );
      await utils.sleep(1000 * 10);
    }
  }

}

module.exports = BcRefresh;
